package com.fzrskj.cloud.free.sys.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import ms.core.object.CObject;

import java.io.*;

/**
 * NavigationBuildUtils.
 *
 * @author zj.
 *         Created on 2019/5/9 0009.
 */
public class NavigationBuildUtils extends CObject {
    /**
     * 导航设置：生成.
     * @param navigation 导航数据.
     * @param rootDir 当前目录.
     * @return
     */
    public boolean build(String navigation, String rootDir) {
        JSONArray navigationAll = JSONArray.parseArray(navigation);
        JSONObject nav = navigationAll.getJSONObject(0);
        String tmpDir = DicParamUtils.getParamValue(DicParamEnum.TEMPLATE_KEY.getVal());
        StringBuilder html = new StringBuilder("<ul>");
        Integer i = 0;
        for (Object subObject : nav.getJSONArray("children")) {
            JSONObject sub = (JSONObject) subObject;
            String linkTop = this.getLink(tmpDir, sub);
            html.append("\n\t<li id=\"m").append(++i).append("\" class=\"m\">").append("<a href=\"").append(linkTop)
                    .append("\" class=\"aa").append(i).append(" mmm\">")
                    .append(sub.getString("label")).append("</a>");
            JSONArray children = sub.getJSONArray("children");
            if (null != children && !children.isEmpty()) {
                html.append("\n\t\t<div class=\"sub\">\n").append("\t\t\t<div class=\"nav_xiao\">\n")
                        .append("\t\t\t\t<div class=\"xiao_five2\">");
                Integer secNum = 0;
                for (Object secObj : children) {
                    JSONObject sec = (JSONObject) secObj;
                    secNum++;
                    String marginLeft = "10px";
                    /*if (secNum == 1) {
                        marginLeft = "80px";
                    }*/

                    String linkSec = this.getLink(tmpDir, sec);

                    //间距修改
                    Integer width = 300;
                    if (1 == sec.getInteger("type")) {
                        width = 160;
                    }

                    JSONArray childrenThreeIF = sec.getJSONArray("children");
                    if(null != childrenThreeIF && !childrenThreeIF.isEmpty()){
                        width = 300;
                    }

                    String floatDirect = "left";
                    if (2 == sec.getInteger("type") ) {
                        width = sec.getInteger("imgWidth") + 20;
                        floatDirect = "left";
                    }
                    String widthString = String.valueOf(width);
                    html.append("\n\t\t\t\t\t<div style=\"width:").append(widthString)
                            .append("px; float:").append(floatDirect).append("; margin-").append(floatDirect)
                            .append(":").append(marginLeft).append("\">");

                    if (1 == sec.getInteger("type")) {
                        html.append("<a style=\"width: ").append(widthString).append("px; text-align:left;\" href=\"")
                                .append(linkSec).append("\">").append(sec.getString("label"))
                                .append("</a>");
                    }

                    if (2 == sec.getInteger("type")) {
                        html.append("<a href=\"").append(linkSec).append("\">")
                                .append("<div style=\"width:").append(sec.getString("imgWidth"))
                                .append("px; text-align:center; float:left\"><img src=\"")
                                .append(sec.getString("img")).append("\" border=0 width=\"")
                                .append(sec.getString("imgWidth")).append("\" height=\"")
                                .append(sec.getString("imgHeight")).append("\" /></div>")
                                .append("<div style=\"width:").append(sec.getString("imgWidth"))
                                .append("px; text-align:center; float:left\">")
                                .append(sec.getString("label")).append("</div>").append("</a>");
                    }

                    if (3 == sec.getInteger("type")) {
                        html.append("<a style=\"width:").append(widthString).append("px; text-align:left;\" href=\"")
                                .append(linkSec).append("\" style=\"line-height:")
                                .append(sec.getString("imgHeight")).append("px\">")
                                .append("<img src=\"").append(sec.getString("img")).append("\" border=\"0\" ")
                                .append(" width=\"").append(sec.getString("imgWidth")).append("\" height=\"")
                                .append(sec.getString("imgHeight")).append("\" align=\"middle\" />")
                                .append(sec.getString("label")).append("</a>");
                    }

                    JSONArray childrenThree = sec.getJSONArray("children");
                    if (null != childrenThree && !childrenThree.isEmpty()) {

                        html.append("<ul style=\"border-top:1px solid #e3e3e3; padding: 10px\">");

                        for (Object threeObject : childrenThree) {
                            JSONObject three = (JSONObject) threeObject;
                            Integer widthThree = width - 30;
                            String linkThree = this.getLink(tmpDir, three);
                            html.append("<li  style=\"width: ").append(String.valueOf(widthThree)).append("px\">");
                            if (1 == three.getInteger("type")) {
                                html.append("<a href=\"").append(linkThree).append("\">")
                                        .append(three.getString("label")).append("</a>");
                            }

                            if (2 == three.getInteger("type")) {
                                html.append("<a href=\"").append(linkThree).append("\">")
                                        .append("<div style=\"width:").append(three.getString("imgWidth"))
                                        .append("px; text-align:center; float:left\"><img src=\"")
                                        .append(three.getString("img")).append("\" border=0 width=\"")
                                        .append(three.getString("imgWidth")).append("\" height=\"")
                                        .append(three.getString("imgHeight")).append("\" /></div>")
                                        .append("<div style=\"width:").append(three.getString("imgWidth"))
                                        .append("px; text-align:center; float:left\">")
                                        .append(three.getString("label")).append("</div>").append("</a>");
                            }

                            if (3 == three.getInteger("type")) {
                                html.append("\n\t\t\t\t\t<a class=\"suba\" href=\"").append(linkThree)
                                        .append("\" style=\"line-height:").append(three.getString("imgHeight")).append("px\">")
                                        .append("<img src=\"").append(three.getString("img")).append("\" border=\"0\" ")
                                        .append(" width=\"").append(three.getString("imgWidth")).append("\" height=\"")
                                        .append(three.getString("imgHeight")).append("\" align=\"middle\" />")
                                        .append(three.getString("label")).append("</a>");
                            }

                            html.append("</li>");
                        }

                        html.append("</ul>");
                    }

                    html.append("</div>");
                }
                html.append("\n\t\t\t\t</div>").append("\n\t\t\t</div>").append("\n\t\t</div>");
            }
            html.append("\n\t</li>");
        }
        html.append("\n</ul>");
        html.append("\n<script type=\"text/javascript\">jQuery(\".nav\").slide({ type:\"menu\",  titCell:\".m\", targetCell:\".sub\", effect:\"slideDown\", delayTime:300, triggerTime:100,returnDefault:true});</script>");
        html.append("\n<script language=\"JavaScript\" src=\"").append(tmpDir).append("js/nav.js\"></script>");
        html.append("\n<script language=\"JavaScript\" src=\"").append(tmpDir).append("js/jquery.SuperSlide.2.1.1.js\"></script>");
        this.save(html.toString(), rootDir);
        return true;
    }

    /**
     * 生成链接.
     * @param tmpDir 模板目录.
     * @param node 导航节点信息.
     * @return link.
     */
    private String getLink(String tmpDir, JSONObject node) {
        String link = node.getString("link");
        if (link.toLowerCase().indexOf("javascript") > -1) {
            return link;
        }
        if (link.toLowerCase().startsWith("http")) {
            return link;
        }
        if ("#".equals(link) || "".equals(link)) {
            return link;
        }
        if ("/".equals(link)) {
            return tmpDir;
        }
        return tmpDir + link;
    }

    /**
     * 保存文件.
     * @param content 导航html.
     * @param rootDir 运行目录.
     */
    private void save(String content, String rootDir) {
        BufferedWriter writer = null;
        try {
            //目标文件组成
            String path = DicParamUtils.getParamValue(DicParamEnum.TEMPLATE_KEY.getVal()) + "tpl/nav.html";
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            path = rootDir + path;
            //检查是否存在，不存在创建.
            File file = new File(path);
            if(!file.exists()){
                file.createNewFile();
            }
            //写内容
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            writer.write(content);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
